package com.xique.park.controller.client.web.controller;

import com.github.pagehelper.PageHelper;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.park.bean.request.client.ClientCancelPassRequest;
import com.xique.park.bean.request.client.ClientCashPassListRequest;
import com.xique.park.bean.request.client.ClientHandOutInfoRequest;
import com.xique.park.bean.request.client.ClientSpaceEditRequest;
import com.xique.park.bean.request.comm.ParkOnEditPlateSaveRequest;
import com.xique.park.bean.vo.client.ClientCashPassListVo;
import com.xique.park.controller.client.web.bean.request.*;
import com.xique.park.controller.client.web.service.biz.service.IClientHomeBizService;
import com.xique.park.service.biz.service.IClientBizService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shd
 * @create 2021/7/22 13:46
 */
@RestController
@RequestMapping("/home")
@Slf4j
public class ClientHomeController extends BaseController {

    @Autowired
    private IClientHomeBizService clientHomeBizService;

    @Autowired
    private IClientBizService clientBizService;

    /**
     * 岗亭列表
     * @author shd
     * @date 2021/7/29 10:34
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("/gt/list")
    @ApiOperation("岗亭列表")
    public AjaxResult gtList() {
        return clientHomeBizService.getClientList(SecurityUtils.getUserId());
    }

    /**
     * 选择岗亭
     * @author shd
     * @date 2021/7/29 10:34
     * @param request /
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @PostMapping("selectClient")
    @ApiOperation("选择岗亭")
    public AjaxResult selectClient(@Validated @RequestBody ClientHomeSelectClientRequest request) {
        return clientHomeBizService.selectClient(request);
    }

    /**
     * 岗亭首页信息
     * @author shd
     * @date 2021/7/29 10:34
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("homePageInfo")
    @ApiOperation("岗亭首页")
    public AjaxResult homePageInfo() {
        return clientBizService.getHomePageInfo(SecurityUtils.getClientId().toString());
    }

    /**
     * 岗亭设备列表
     * @author shd
     * @date 2021/7/29 10:34
     * @param request /
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("deviceList")
    @ApiOperation("岗亭设备列表")
    public AjaxResult deviceList(ClientDeviceListRequest request) {
        request.setClientId(SecurityUtils.getClientId().toString());
        return new AjaxResult(clientBizService.getClientdeviceList(request.getClientId()));
    }

    @PostMapping("lane/config/edit")
    @ApiOperation("岗亭通道配置修改")
    public AjaxResult editClientLaneConfig(@Validated @RequestBody WebClientLaneConfig request) {
        return clientHomeBizService.editClientLaneConfig(request);
    }

    @GetMapping("lane/detail")
    @ApiOperation("通道明细")
    public AjaxResult lanDetail(@RequestParam("laneId") Long laneId) {
        return clientHomeBizService.lanDetail(laneId);
    }


    @PostMapping("handCarInOrOut")
    @ResponseBody
    @ApiOperation("手动进出场")
    @Log(title = "手动进出场", businessType = BusinessType.COMMAND)
    public AjaxResult handCarInOrOut(@Validated @RequestBody WebClientHandInOutRequest request){

//        boolean matches = request.getPlate().matches("[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-HJ-NP-Z0-9]{4}[A-Z0-9挂学警港澳]{1,2}$");
        boolean matches = StringUtils.isLegalPlate(request.getPlate());
        if (!matches){
            return  AjaxResult.error("车牌格式不正确!");
        }
        return AjaxResult.success(clientHomeBizService.handCarInOrOut(request));
    }

    @PostMapping("plate/edit")
    @ApiOperation("在场记录车牌修改")
    public AjaxResult editParkOnPlate(@Validated @RequestBody ParkOnEditPlateSaveRequest request) {
        boolean matches = StringUtils.isLegalPlate(request.getNewPlate());
        if (!matches){
            return  AjaxResult.error("车牌格式不正确!");
        }
        return  clientHomeBizService.editParkOnPlate(request);
    }


    @GetMapping("parkOn/getUrl")
    @ApiOperation("在场列表获取入场图片")
    public AjaxResult getParkOnUrl(@RequestParam("recordId") String recordId ) {
        return clientHomeBizService.getParkOnUrl(recordId);
    }

    @PostMapping("handOut/info")
    @ApiOperation("手动出场信息查询")
    public AjaxResult getHandOutInfo(@Validated @RequestBody ClientHandOutInfoRequest request ) {
        return clientBizService.handOutInfo(request);
    }


    @GetMapping("out/cancelPass")
    @ApiOperation("取消放行")
    @Log(title = "取消放行", businessType = BusinessType.COMMAND)
    public AjaxResult cancelPass( @Validated ClientCancelPassRequest request ) {
        request.setParkId(SecurityUtils.getParkId().toString());
        return clientBizService.cancelPass(request);
    }

    @PostMapping("pass/cash")
    @ApiOperation("现金放行")
    @Log(title = "现金放行", businessType = BusinessType.COMMAND)
    public AjaxResult cashPass(@Validated @RequestBody ClientCashOrFreePassRequest request) {
        return clientHomeBizService.cashPass(request);
    }

    @PostMapping("pass/free")
    @ApiOperation("免费放行")
    @Log(title = "免费放行", businessType = BusinessType.COMMAND)
    public AjaxResult freePass(@Validated @RequestBody ClientCashOrFreePassRequest request) {
        return clientHomeBizService.freePass(request);
    }

    /**
     * 车位信息
     * @author shd
     * @date 2021/8/10 9:48
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("space")
    @ApiOperation("车位信息")
    public AjaxResult spaceInfo() {
        return clientHomeBizService.getSpaceInfo();
    }

    /**
     * 修改剩余车位
     * @author shd
     * @date 2021/8/10 9:48
     * @param request /
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @PostMapping("space/edit")
    @ApiOperation("修改剩余车位")
    public AjaxResult spaceEdit(@Validated @RequestBody ClientSpaceEditRequest request) throws Exception {
        request.setParkId(SecurityUtils.getParkId());
        return clientBizService.spaceEdit(request);
    }

    /**
     * 通道信息
     * @author shd
     * @date 2021/8/10 9:51
     * @param deviceId /
     * @return com.xique.common.core.web.domain.AjaxResult
     */
    @GetMapping("lane/info")
    @ApiOperation("通道信息")
    public AjaxResult laneInfo(@RequestParam("deviceId") Long deviceId) {
        return clientBizService.getLaneInfo(deviceId);
    }

    @PostMapping("record/cashPass")
    @ApiOperation("现金收费记录")
    public AjaxResult cashPassList(@Validated @RequestBody ClientCashPassListRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ClientCashPassListVo> listVos = clientBizService.clientCashPassList(request);
        return new AjaxResult(getDataTable(listVos));
    }

    @GetMapping("parkIn/detail")
    @ApiOperation("入场详情")
    public AjaxResult parkInDetail(@RequestParam("inId") Long inId) {
        return clientBizService.selectParkInDetail(inId);
    }

    @GetMapping("parkOut/detail")
    @ApiOperation("出场详情")
    public AjaxResult parkOutDetail(@RequestParam("outId") Long outId) {
        return clientBizService.selectParkOutDetail(outId);
    }

    @PostMapping("confirmIn")
    @ApiOperation("确认入场")
    public AjaxResult confirmIn(@Validated @RequestBody ClientConfirmInRequest request) {
        return clientHomeBizService.confirmIn(request);
    }

    /***************************add by xl.liu 2022/01/06****************************/

    @PostMapping("handOut/parkOnList")
    @ApiOperation("手动出场根据车牌号获取在场车辆列表")
    public AjaxResult handOutParkOnList(@Validated @RequestBody HandOutParkOnListRequest request){
        return clientHomeBizService.getHandOutParkOnList(request);
    }

    @GetMapping("handIn/isAllowEditInTime")
    @ApiOperation("手动入场是否允许修改入场时间")
    public AjaxResult isAllowEditInTime(){
        return clientHomeBizService.isAllowEditInTime();
    }

    @GetMapping("freePass/reason")
    @ApiOperation("获取免费放行原因列表")
    public AjaxResult getFreePassReason(){
        return clientHomeBizService.getFreePassReason();
    }

    @GetMapping("openGate/reason")
    @ApiOperation("获取开闸原因列表")
    public AjaxResult getOpenGateReason(){
        return clientHomeBizService.getOpenGateReason();
    }

    @GetMapping("closeGate/reason")
    @ApiOperation("获取关闸原因列表")
    public AjaxResult getCloseGateReason(){
        return clientHomeBizService.getCloseGateReason();
    }

}

